using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.IO;
using System.Web.Security;
using System.Data.Entity;
using DomainModel.Concrete;
using DomainModel.Entities;

namespace WebUI.Controllers
{

    public class VehicleCategoryController : Controller
    {
        SqlVehicleCategoryRepository _vehicleCategoryRepository;
        #region PrivateFunctions
        private ActionResult HandleMyError(Exception ex, string actionName)
        {
            var modelErr = new HandleErrorInfo(ex, "VehicleCategoryController", actionName);
            return View("Error", modelErr);
        }
        #endregion
        public VehicleCategoryController() { _vehicleCategoryRepository = new SqlVehicleCategoryRepository(); }
        #region PublicActions
        public ActionResult Index()
        {
            try
            {
                return View(_vehicleCategoryRepository.GetAll());
            }
            catch (Exception ex)
            {
                return HandleMyError(ex, "CreateAction");
            }

        }

        public ActionResult Details(int id)
        {
            try
            {
                return View(_vehicleCategoryRepository.GetById(id));
            }
            catch (Exception ex)
            {
                return HandleMyError(ex, "CreateAction");
            }
        }

        public ActionResult Create()
        {
            try
            {
                VehicleCategory vehicleCategory = new VehicleCategory();
                return View(vehicleCategory);
            }
            catch (Exception ex)
            {
                return HandleMyError(ex, "CreateAction");
            }

        }

        [HttpPost]
        public ActionResult Create(VehicleCategory vehiclecategory, FormCollection collection)
        {
            vehiclecategory.LastUpdate = DateTime.Now;
            vehiclecategory.IsApproved = false;
            vehiclecategory.UserName = User.Identity.Name;

            if (ModelState.IsValid)
            {
                try
                {
                    _vehicleCategoryRepository.Insert(vehiclecategory);
                    return RedirectToAction("Index");
                }
                catch (Exception ex)
                {
                    return HandleMyError(ex, "CreateAction");
                }
            }
            else
            {
                return RedirectToAction("Index");
            }
        }

        public ActionResult Edit(int id)
        {

            return View(_vehicleCategoryRepository.GetById(id));
        }

        [HttpPost]
        public ActionResult Edit(VehicleCategory vehiclecategory, int id, FormCollection collection)
        {
            vehiclecategory.LastUpdate = DateTime.Now;
            vehiclecategory.IsApproved = false;
            vehiclecategory.UserName = User.Identity.Name;

            if (ModelState.IsValid)
            {
                try
                {

                    _vehicleCategoryRepository.Update(vehiclecategory);
                    return RedirectToAction("Index");
                }
                catch (Exception ex)
                {
                    return HandleMyError(ex, "EditAction");
                }
            }
            else
            {
                return RedirectToAction("Index");
            }
        }

        public ActionResult Delete(int id)
        {
            return View(_vehicleCategoryRepository.GetById(id));
        }

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                _vehicleCategoryRepository.Delete(_vehicleCategoryRepository.GetById(id));
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                return HandleMyError(ex, "DeleteAction");
            }
        }
        #endregion
    }
}
